/**
 * 
 */
package org.myOrg.spetrum.GUI;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JToolBar;

import org.myOrg.spetrum.Listener.RunListener;
import org.myOrg.spetrum.spectrumStruct.SpectrumFactory;
import org.myOrg.spetrum.util.Message;
import org.myOrg.spetrum.util.MessageImplement;
import org.myOrg.spetrum.util.MessageMangement;
import org.myOrg.spetrum.util.MessageMangementImplement;
import org.myOrg.spetrum.util.OverallFactory;
import org.myOrg.spetrum.util.Reciver;
import org.myOrg.spetrum.util.Sender;

/**
 * @author Xintao Niu
 * 
 */
public class ToolBar extends JToolBar implements Reciver, Sender,
		ActionListener {
	public static final String BUGINSERT = "bug insert control";
	public static final String RUNINGVIEW = "view running info";
	private MessageMangement mangerment;

	/**
	 * @return the alogrithmChoose
	 */
	public JComboBox getAlogrithmChoose() {
		return alogrithmChoose;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JButton viewChange;

	/**
	 * @return the viewChange
	 */
	public JButton getViewChange() {
		return viewChange;
	}

	private JButton runButton;
	private JComboBox alogrithmChoose;

	public ToolBar() {
		this.setMessageMangement(OverallFactory.getMessageManger());
		this.register(MessageMangementImplement.ADDBUGMODEL,
				OverallFactory.getMessageManger());
		this.register(MessageMangementImplement.NOBUGMODEL,
				OverallFactory.getMessageManger());
		build();
	}

	private void build() {
		viewChange = new JButton(BUGINSERT);
		
		alogrithmChoose = new JComboBox(SpectrumFactory.NAMES);
		alogrithmChoose.addItem("all");
		alogrithmChoose.addActionListener(this);
		
		runButton = new JButton("run");
		runButton.setEnabled(false);
		runButton.addActionListener(new RunListener());
		runButton.addActionListener(this);

		this.setLayout(new FlowLayout(FlowLayout.LEADING));
		this.add(viewChange);
		this.addSeparator();
		this.add(runButton);
		this.addSeparator();
		this.add(alogrithmChoose);

	}

	/**
	 * @return the button
	 */
	public JButton getButton() {
		return runButton;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JFrame frame = new JFrame();
		frame.setSize(500, 150);
		frame.getContentPane().setLayout(new BorderLayout());
		frame.getContentPane().add(new ToolBar(), BorderLayout.NORTH);
		frame.setVisible(true);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	@Override
	public void register(int messageType, MessageMangement messageManger) {
		// TODO Auto-generated method stub
		messageManger.register(this, messageType);
	}

	@Override
	public void handleMessage(Message msg) {
		// TODO Auto-generated method stub
		if (msg.getType() == MessageMangementImplement.ADDBUGMODEL) {
			this.runButton.setEnabled(true);
		} else if (msg.getType() == MessageMangementImplement.NOBUGMODEL) {
			this.runButton.setEnabled(false);
		}

	}

	@Override
	public void sendMessage(Message msg) {
		// TODO Auto-generated method stub
		mangerment.dispatchMeesage(msg);
	}

	@Override
	public void setMessageMangement(MessageMangement mangerment) {
		// TODO Auto-generated method stub
		this.mangerment = mangerment;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getActionCommand().equals("run")){
			this.viewChange.setText(ToolBar.BUGINSERT);
			return;
		}
		JComboBox choose = (JComboBox) e.getSource();
		Message msg = new MessageImplement(this,
				MessageMangementImplement.CHANGEALOGRITHM, new Integer(choose.getSelectedIndex()));
		sendMessage(msg);
	}

}
